# _*_coding:utf-8_*_
"""
   ATS 1.0(Automated Test Server, Version 1.0) Project Plan File Parser,
   Developed by Ivy.Li 2013.03.18.
   (C)Copyright 2013 Hech. All rights reserved
"""
import configparser as ConfigParser
import os
from ATS.util import constant
from . import sspf
from . import wdf

# error messages define
# error code>1000, means the error will not stop the parsing process. Only record the error code.
No_WAFERs_ERR = 11
IVAL_WAFERS_FORMAT = 12
NO_WDF_FILE = 13
NO_SSPF = 14


class PPF:
    """
    header: {
    'Name': 'ATS Project Plan  File',
    'Version':'1.0',
    'Operator':'Ivy',
    'Process':,Some kind of professional test process',
    'Create Date':'14/03/2013'
    }


    wafers: test wafer IDs: 1,3,5....
    wdf,gdf,pmf,sspf: file name

    csset_uaps:{
    UAP_LOT_BEGIN: *.py
    UAP_LOT_END: *.py
    ...
    }

    wafer_plans={

    {1}:{'WDF':'*.wdf',
         'GDF':'*.gdf',
         'SSPF':'*.sspf',
         'uaps':{}
         }
    {2}:....
    }

    """

    def __init__(self, ppf=None):
        if ppf:
            self.ppf_file = ppf
            self.load(ppf)
        else:
            self.clear()

    def clear(self):
        self.header = {}
        self.wafers = []
        self.wdf = None
        self.gdf = None
        self.pmf = None
        self.sspf = None
        self.casset_uaps = {}
        self.wafer_plans = {}
        self.error = 0

    def clone(self, new_ppf):
        cp = ConfigParser.ConfigParser()
        cp.read(self.ppf_file)
        cp.set('Cassette Plan', 'sspf', 'inking.sspf')
        cp.write(open(new_ppf, 'w'))

    def load(self, ppf):
        self.clear()

        cp = ConfigParser.ConfigParser()
        cp.read(ppf)

        # get header
        keys = ['Name', 'Version', 'Operator', 'Process', 'Create Date']
        for k in keys:
            self.header[k] = cp.get('Header', k)

        # get cassete plan
        Wafers = cp.get('Cassette Plan', 'Wafers')
        if not Wafers:
            self.error = No_WAFERs_ERR
            return
        if Wafers.lower() == 'all':
            for i in range(1, 26, 1):
                self.wafers.append(i)
        else:
            try:
                self.wafers = eval(Wafers)
            except:
                self.error = IVAL_WAFERS_FORMAT
                return
        try:
            self.wdf = cp.get('Cassette Plan', 'WDF')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            self.error = NO_WDF_FILE
        try:
            self.gdf = cp.get('Cassette Plan', 'GDF')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            self.gdf = None
        try:
            self.pmf = cp.get('Cassette Plan', 'PMF')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            self.pmf = None
        try:
            self.sspf = cp.get('Cassette Plan', 'SSPF')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            self.error = NO_SSPF
            return

        try:
            uap = cp.get('Cassette Plan', 'UAP_LOT_BEGIN')
            if uap:
                self.casset_uaps['UAP_LOT_BEGIN'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_LOT_END')
            if uap:
                self.casset_uaps['UAP_LOT_END'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_WAFER_BEGIN')
            if uap:
                self.casset_uaps['UAP_WAFER_BEGIN'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_WAFER_END')
            if uap:
                self.casset_uaps['UAP_WAFER_END'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_SITE_BEGIN')
            if uap:
                self.casset_uaps['UAP_SITE_BEGIN'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_SITE_END')
            if uap:
                self.casset_uaps['UAP_SITE_END'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        try:
            uap = cp.get('Cassette Plan', 'UAP_USER_ABORT')
            if uap:
                self.casset_uaps['UAP_USER_ABORT'] = uap
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            pass

        for w in self.wafers:
            tem_d = {}
            self.wafer_plans[w] = tem_d
            sec_name = 'Wafer Plan %s' % w
            try:
                tem_d['WDF'] = cp.get(sec_name, 'WDF')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                tem_d['WDF'] = None
            try:
                tem_d['GDF'] = cp.get(sec_name, 'GDF')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                tem_d['GDF'] = None
            try:
                tem_d['SSPF'] = cp.get(sec_name, 'SSPF')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                tem_d['SSPF'] = None

            tem_d['uaps'] = {}
            try:
                tem_d['uaps']['UAP_WAFER_BEGIN'] = cp.get(sec_name, 'UAP_WAFER_BEGIN')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                pass
            try:
                tem_d['uaps']['UAP_WAFER_END'] = cp.get(sec_name, 'UAP_WAFER_END')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                pass
            try:
                tem_d['uaps']['UAP_SITE_BEGIN'] = cp.get(sec_name, 'UAP_SITE_BEGIN')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                pass
            try:
                tem_d['uaps']['UAP_SITE_END'] = cp.get(sec_name, 'UAP_SITE_END')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                pass
            try:
                tem_d['uaps']['UAP_UAER_ABORT'] = cp.get(sec_name, 'UAP_UAER_ABORT')
            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                pass


def get_sspf_obj(paths):
    ppf_obj = PPF(paths)
    if ppf_obj.error > 0:
        if ppf_obj.error > 1000:
            return None
        else:
            return None

    if len(ppf_obj.wafers) < 1:
        return None
    w = ppf_obj.wafers[0]
    wp_dict = ppf_obj.wafer_plans
    if w in wp_dict:  ##
        wdf_file = wp_dict[w]['WDF']
        if wdf_file is None:
            wdf_file = ppf_obj.wdf
        sspf_file = wp_dict[w]['SSPF']
        if sspf_file is None:
            sspf_file = ppf_obj.sspf
    #                uaps=wp_dict[w]['uaps']
    #                if not uaps:
    #                    uaps=ppf_obj.casset_uaps
    else:
        wdf_file = ppf_obj.wdf
        sspf_file = ppf_obj.sspf
    #                uaps=ppf_obj.casset_uaps

    if sspf_file and sspf_file is not None:
        abs_path = os.path.join(constant.SSPF_PATH, sspf_file)
        sspf_obj = sspf.SSPF(abs_path)
        if 0 < sspf_obj.error < 1000:
            return None
        if sspf_obj.error > 0 and sspf_obj.error > 1000:
            return None
    else:
        return None

    if wdf_file and wdf_file is not None:
        abs_path = os.path.join(constant.WDF_PATH, wdf_file)
        wdf_obj = wdf.WDF(abs_path)
        if 0 < wdf_obj.error < 1000:
            wdf_obj = None
    else:
        wdf_obj = None
    if wdf_obj:
        sspf_obj.set_ssites(wdf_obj.ssites_seq)
    if sspf_obj.tests is None:
        return None

    return sspf_obj

##Test code
# ppf=PPF(r'D:\Eclipse_Home\MRBIG_Cust\plans\ppf\cust1.ppf')
# ppf.clone(r'D:\Eclipse_Home\MRBIG_Cust\plans\ppf\cust1_inking.ppf')
# print ppf.header
# print ppf.wafers
# print ppf.pmf
# print ppf.sspf
# print ppf.wdf
# print ppf.gdf
# print ppf.error,'===========error'
# for key in ppf.casset_uaps:
#    print key,'<--------->',ppf.casset_uaps[key]
# for key in ppf.wafer_plans:
#    print key,'<--------->',ppf.wafer_plans[key]
